package com.midea.ai.air.ui.more.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.more.data.MyFamily;
import com.midea.ai.air.view.AlertCenterDialog;
import com.midea.ai.air.view.CustomSwipeListView;
import com.midea.ai.air.view.SwipeItemView;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/16.
 * 我加入的家庭Adapter
 */
public class MyFamilyAdapter extends ArrayAdapter<MyFamily> {
    private LayoutInflater mLayoutInflater;
    private SwipeItemView mLastSlideViewWithStatusOn;
    private Context mContext;

    public MyFamilyAdapter(Context context, int resource, List<MyFamily> objects) {
        super(context, resource, objects);
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final MyFamily item = getItem(position);
        SwipeItemView swipeItemView = (SwipeItemView) convertView;
        if(swipeItemView == null) {
            View view = mLayoutInflater.inflate(R.layout.item_list_family, null);
            swipeItemView = new SwipeItemView(mContext);
            swipeItemView.setContentView(view);
            holder = new ViewHolder();
            holder.ivHeadIcon = (ImageView) swipeItemView.findViewById(R.id.imgView_icon);
            holder.tvName = (TextView) swipeItemView.findViewById(R.id.txtView_name);
            holder.botLine = swipeItemView.findViewById(R.id.bottom_line);
            holder.ivIsSelected = (ImageView) swipeItemView.findViewById(R.id.imgView_select);
            holder.delete = (ViewGroup) swipeItemView.findViewById(R.id.holder);

            swipeItemView.setOnSlideListener(new SwipeItemView.OnSlideListener() {
                @Override
                public void onSlide(View view, int status) {
                    if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
                        mLastSlideViewWithStatusOn.shrink();
                    }
                    if (status == SLIDE_STATUS_ON) {
                        mLastSlideViewWithStatusOn = (SwipeItemView) view;
                    }
                }
            });

            swipeItemView.setTag(holder);
        } else {
            holder = (ViewHolder)swipeItemView.getTag();
        }

        if (CustomSwipeListView.mFocusedItemView != null) {
            CustomSwipeListView.mFocusedItemView.shrink();
        }

        holder.ivHeadIcon.setImageResource(item.getHeadIconResId());
        holder.tvName.setText(item.getStrName());
        if(item.isSelected()) {
            holder.ivIsSelected.setVisibility(View.VISIBLE);
        } else {
            holder.ivIsSelected.setVisibility(View.INVISIBLE);
        }
        if(position == getCount()-1) {
            holder.botLine.setVisibility(View.INVISIBLE);
        } else {
            holder.botLine.setVisibility(View.VISIBLE);
        }

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertCenterDialog(mContext).builder().setTitle(mContext.getString(R.string.air_more_family_exit))
                        .setMsg(mContext.getString(R.string.air_more_family_exit_msg)+item.getStrName()+mContext.getString(R.string.air_more_family_change_msg_end))
                        .setPositiveButton("", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .setNegativeButton("", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .show();
            }
        });
        return swipeItemView;
    }

    private class ViewHolder {
        public ImageView ivHeadIcon;
        public TextView tvName;
        private ImageView ivIsSelected;
        private View botLine;
        private ViewGroup delete;
    }
}
